/// <reference path="F:\Html5 Game\Game project\js\libs\kangax-fabric-48.306.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\DBAbstractionLayer.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\GameCanvas.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Node.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\NodeType.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Planet.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Region.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Scale.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\System.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Universe.js"/>
/// <reference path="F:\Html5 Game\Game project\js\classes\Utilities.js"/>
(function ($) {

    //Scherm de code af met een functie zodat we geen globale variabelen hebben en de functies af kunnen schermen
    this.GameCanvas = function (elem, scale) {
        //Verwijzingen
        var gameCanvas = $(elem);
        var obj = this;

        //Variabelen
        //var context = null;
        //var context = new fabric.Canvas(elem);
        var context = gameCanvas[0];
        //, CANVAS_HEIGHT: 480, CANVAS_WIDTH: 820
        context.width = 820;
        context.height = 480;

        var pause = true;
        var gameLoop = null;
        var scale = scale;
        var dbl = new DBAbstractionLayer();
        var regions = dbl.getRegions();
        var universe = dbl.getUniverses(regions);

        var _constructor = function () {

        }

        //Public functies
        obj.draw = function () {
            universe.draw(context, Scale.UNIVERSE);
        }

        obj.start = function () {
            obj.unpause();
        }

        obj.pause = function () {
            if (gameLoop) {
                clearTimeout(gameLoop);
            }
        }

        obj.unpause = function () {
            _loop();
        }


        //Private Functies
        var _loop = function () {
            _clear();

            //Starttijd vastleggen
            var d = new Date();
            var startTime = d.getTime();

            //Do game stuff here
            obj.draw();

            //Eindtijd vastleggen
            var d = new Date();
            var endTime = d.getTime();

            //Wachttijd tot de volgende loop berekenen
            var currentDuration = endTime - startTime;
            var loopDuration = 1000 / 20;

            var waitTime = loopDuration - currentDuration;
            if (waitTime < 0) {
                waitTime = 0;
            }

            //Volgende loop inplannen
            obj.gameLoop = setTimeout(function () {
                //_loop();
            }, waitTime);
        }

        // Clear the canvas
        var _clear = function () {
            context.fillStyle = '#000000';
            context.clearRect(0, 0, gameCanvas.width(), gameCanvas.height());
            context.beginPath();
            context.rect(0, 0, gameCanvas.width(), gameCanvas.height());
            context.closePath();
            context.fill();
        }

        _constructor();
    };

})(jQuery);